﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Helpers
{
    public class StringHelper
    {
        public static bool IsnullOrBlank(string src)
        {
            return src == null || src.Trim() == string.Empty;
        }

        public static bool IsContains(string src, string part, params StringCompareModifications[] modifications)
        {
            bool result = false;

            if (src == null)
                return part == null;

            if(part == null)
                return false;

            if (src == string.Empty)
                return part == string.Empty;

            if (part == string.Empty)
                return false;

            string modifiedSrc = src;
            string modifiedPart = part;

            if (!CollectionHelper.IsNullOrEmpty(modifications))
            {
                foreach (StringCompareModifications modification in modifications)
                {
                    switch (modification)
                    {
                        case StringCompareModifications.RemoveCase:
                            modifiedSrc = modifiedSrc.ToUpper();
                            modifiedPart = modifiedPart.ToUpper();
                            break;
                        case StringCompareModifications.RemoveDoubleSpaces:
                            modifiedSrc = RemoveDoubleSpaces(modifiedSrc);
                            modifiedPart = RemoveDoubleSpaces(modifiedPart);
                            break;
                        case StringCompareModifications.RemoveSpaces:
                            modifiedSrc = modifiedSrc.Replace(" ", string.Empty);
                            modifiedPart = modifiedPart.Replace(" ", string.Empty);
                            break;
                        case StringCompareModifications.RemovenewLines:
                            modifiedSrc = modifiedSrc.Replace(Environment.NewLine, string.Empty);
                            modifiedPart = modifiedPart.Replace(Environment.NewLine, string.Empty);
                            break;
                        default:
                            throw new Exception("Unknown type of StringCompareModifications");
                    }
                }
            }

            result = modifiedSrc.Contains(modifiedPart);

            return result;
        }

        public static string RemoveDoubleSpaces(string src)
        {
            if (src == null || src == string.Empty)
                return src;

            while (src.IndexOf("  ") >= 0)
            {
                src = src.Replace("  ", " ");
            }

            return src;
        }
    }

    public enum StringCompareModifications
    {
        RemoveCase = 1,
        RemoveDoubleSpaces = 2,
        RemoveSpaces = 4,
        RemovenewLines = 8
    }
}
